cmake_minimum_required(VERSION 3.10)

# 设置项目名称和语言
project(MultiPhaseSimulate CXX)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 添加宏定义
add_definitions(-DGLOG_USE_GLOG_EXPORT)
add_definitions(-D_USE_MATH_DEFINES)

# 设置优化等级 (使用 MSVC 的优化选项)
if(MSVC)
    add_compile_options(/O2)
    # 使用多处理器编译
    add_compile_options(/MP)
    # 禁用特定警告
    add_compile_options(/wd4251 /wd4275)
endif()

# 设置源文件
set(SOURCES
    cell.cpp
    config.cpp
    multi_phase_simulate.cpp
    pipe.cpp
    PT_table.cpp
)

# 设置头文件目录
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/ceres-solver-2.2.0_build/include
    ${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/eigen-3.3.9_build/include/eigen3
    ${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/glog-0.7.0_build/include
    ${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/gflags-2.2.2_build/include
)

# 创建可执行文件
add_executable(${PROJECT_NAME} ${SOURCES})

# 设置库文件路径
set(CERES_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/ceres-solver-2.2.0_build/lib/Release)
set(GFLAGS_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/gflags-2.2.2_build/lib/Release)
set(GLOG_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/glog-0.7.0_build/lib)

# 链接库文件
target_link_libraries(${PROJECT_NAME}
    ${CERES_LIB_DIR}/ceres.lib
    ${GFLAGS_LIB_DIR}/gflags.lib
    ${GFLAGS_LIB_DIR}/gflags_nothreads.lib
    ${GLOG_LIB_DIR}/glog.lib
)

# 复制DLL文件到输出目录
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/gflags-2.2.2_build/bin/Release/gflags_nothreads.dll"
        "${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/gflags-2.2.2_build/bin/Release/gflags.dll"
        "${CMAKE_CURRENT_SOURCE_DIR}/ceres-solver-2.2.0/glog-0.7.0_build/bin/glog.dll"
        $<TARGET_FILE_DIR:${PROJECT_NAME}>
)

# 复制input文件夹到输出目录
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${CMAKE_CURRENT_SOURCE_DIR}/input"
        "${CMAKE_BINARY_DIR}/input"
)

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)